const Service = require('egg').Service;

class hjyUserService extends Service {

    //查询用户登录信息
    async queryUser(t_user) {

        let queryString = 'select nickname,department_id,type,username from t_user where username = ?'
        let params = [t_user.username];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //加载部门信息
    async queryNumDept() {

        let result = await this.app.mysql.select('t_department');
        return result;
    }

    //新增部门信息
    async addNewDept(t_department) {

        t_department.create_time = this.app.mysql.literals.now;
        let result = await this.app.mysql.insert('t_department', {...t_department });
        return result
    }

    //修改部门信息
    async updateUser(t_department) {

        t_department.modified_time = this.app.mysql.literals.now;
        let result = await this.app.mysql.update('t_department', {...t_department }, {
            where: { id: t_department.id }
        });
        return result;
    }

    //删除部门信息
    async deleteDept(t_department) {

        let result = await this.app.mysql.delete('t_department', {...t_department }, {
            where: { id: t_department.id }
        });
        return result
    }

    //部门信息模糊查询
    async queryDept(t_department) {

        let queryString = 'select * from t_department where name like ?'
        let params = [t_department.name + '%'];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //新增物资资料
    async addNewProduct(b_product) {

        b_product.create_time = this.app.mysql.literals.now;
        let result = await this.app.mysql.insert('b_product', {...b_product });
        return result
    }

    //修改物资资料
    async updateProduct(b_product) {

        b_product.modified_time = this.app.mysql.literals.now;
        let result = await this.app.mysql.update('b_product', {...b_product }, {
            where: { id: b_product.id }
        });
        return result;
    }

    //审核通过
    async updateAudit(b_product) {

        let queryString = 'update b_product set status = 1 where status = 2'
        let params = [b_product.id];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //物资回收
    async updateRecycle(b_product) {

        let queryString = 'update b_product set status = 0 where status = 1'
        let params = [b_product.id];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //查询正常物资
    async queryProduct1(b_product) {

        let queryString = 'select * from b_product where status=1'
        let params = [b_product.status];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //查询回收站物资
    async queryProduct0(b_product) {

        let queryString = 'select * from b_product where status=0'
        let params = [b_product.status];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //查询审核物资
    async queryProduct2(b_product) {

        let queryString = 'select * from b_product where status=2'
        let params = [b_product.status];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //物资资料模糊查询
    async queryProduct(b_product) {

        let queryString = 'select * from b_product where name like ?'
        let params = [b_product.name + '%'];
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //物资类别查询
    async queryClass(b_product) {

        let one_category_id = b_product.one_category_id
        let two_category_id = b_product.two_category_id
        let three_category_id = b_product.three_category_id

        if (one_category_id != null) {
            let queryString = 'select * from b_product where one_category_id'
            let params = [one_category_id];
            let result = await this.app.mysql.query(queryString, params);
            return result;
        } else if (two_category_id != null) {
            let queryString = 'select * from b_product where two_category_id'
            let params = [two_category_id];
            let result = await this.app.mysql.query(queryString, params);
            return result;
        } else if (three_category_id != null) {
            let queryString = 'select * from b_product where three_category_id'
            let params = [three_category_id];
            let result = await this.app.mysql.query(queryString, params);
            return result;
        }

    }


    //删除物资资料
    async deleteProduct(b_product) {

        let result = await this.app.mysql.delete('b_product', {...b_product }, {
            where: { id: b_product.id }
        });
        return result
    }


}

module.exports = hjyUserService;